package com.ruoyi.common.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.DES;
import com.ruoyi.common.annotation.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.web.servlet.server.Encoding;

import java.nio.charset.StandardCharsets;
import java.util.Random;

/***************************************
 * @Describe 加密解密工具
 * @author 潘志能
 * @EMail panzhineng@yiview.com
 * @Company 深圳亿维锐创科技股份有限公司
 * @Time 2024/11/22
 *****************************************/
@Slf4j
public class EncryptionUtils {
    public static byte[] Iv64 = {38, 53, 66, 124, 13, 127, 35, 55};

    public static byte[] byKey64 = {22, 127, 1, 3, 88, 38, 33, 111};

    //字符串加密
    public static String SKeyEncrypt(String strText) {
        try {
            DES des = new DES(Mode.CFB, Padding.ZeroPadding, Iv64, byKey64);
            return des.encryptBase64(Base64.encode(strText));

        } catch (Exception ex) {
            ex.printStackTrace();
            return"";
        }
    }

    //字符串加密
    public static String SKeyEncrypt(String id, String saltKey, String strText) {
        try {
            if(StringUtils.isEmpty(strText)){
                return  "";
            }
            String coder = getKey(id, saltKey);
            byte[] bytes = coder.getBytes(StandardCharsets.UTF_8);
            byte[] iv64  =getKeyByte(10,bytes);// coder.substring(10,18).getBytes(StandardCharsets.UTF_8);
            byte[] byKey64 = getKeyByte(bytes.length-9,bytes);// coder.substring(coder.length() - 9,coder.length() - 1).getBytes(StandardCharsets.UTF_8);
            DES des = new DES(Mode.CFB, Padding.ZeroPadding, iv64, byKey64);
            String ret = des.encryptBase64(Base64.encode(strText));
            StringBuilder sb = new StringBuilder(ret);
            Random random = new Random();
            sb.insert(random.nextInt(sb.length() - 1), "$$" + coder + "$$");
            return sb.toString();

        } catch (Exception ex) {
            ex.printStackTrace();
            return"";
        }
    }

    static String getKey(String content, String saltKey) {
        // 将字符串转换为Base64编码的字符串
        //String base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(content));

        String base64String = SKeyEncrypt(content + saltKey).replace("=", "");
        log.info(String.format("{0}==>{1}", content, base64String));


        return base64String;
    }

    //字符串解密
    public static String SKeyDecrypt(String strText) {
        try {
            DES des = new DES(Mode.CFB, Padding.ZeroPadding, Iv64, byKey64);
            String base64 = des.decryptStr(strText);
            return Base64.decodeStr(base64);
        } catch (Exception ex) {
            ex.printStackTrace();
            return"";
        }
    }

    //字符串解密
    public static String decrypt(String strText) {
        try {
            String content = strText;
            String key = "";
            if (strText.contains("$$")) {
                int start = strText.indexOf("$$");
                key = strText.substring(start, strText.lastIndexOf("$$") - start + 2);
                log.info("解密key===>" + key);
                content = strText.replace(key, "").replace("$$", "");
            } else {
                return "";
            }
            key = key.replace("$$", "");
            byte[] bytes = key.getBytes(StandardCharsets.UTF_8);
            byte[] iv64  = getKeyByte(10,bytes);//key.substring(10,18).getBytes(StandardCharsets.UTF_8);
            byte[] byKey64 =  getKeyByte(bytes.length-9,bytes);// key.substring(key.length() - 9,key.length() - 1).getBytes(StandardCharsets.UTF_8);
            DES des = new DES(Mode.CFB, Padding.ZeroPadding, iv64, byKey64);
            String ret = des.decryptStr(content);
            return Base64.decodeStr(ret,StandardCharsets.UTF_8);
        } catch (Exception ex) {
            ex.printStackTrace();
            return"";
        }
    }

    static byte[] getKeyByte(Integer startIndex,byte[] data){
        int length = data.length;
        byte[] keys =new byte[8];
        for(int i =0;i<8;i++){
            keys[i] = data[(startIndex*3*i)/length];
        }

        return keys;
    }

    //字符串解密
    public static String SKeyDecrypt(String id, String saltKey, String strText) {
        try {
            String content = strText;
            if (strText.contains("$$")) {
                int start = strText.indexOf("$$");
                String key = strText.substring(start, strText.lastIndexOf("$$")+ 2);
                log.info("解密key===>" + key);
                content = strText.replace(key, "").replace("$$", "");
            }
            String coder = getKey(id, saltKey);
            byte[] bytes = coder.getBytes(StandardCharsets.UTF_8);
            byte[] iv64  =getKeyByte(10,bytes);// coder.substring(10,18).getBytes(StandardCharsets.UTF_8);
            byte[] byKey64 = getKeyByte(bytes.length-9,bytes);// coder.substring(coder.length() - 9,coder.length() - 1).getBytes(StandardCharsets.UTF_8);
            DES des = new DES(Mode.CFB, Padding.ZeroPadding, iv64, byKey64);
            String ret = des.decryptStr(content,StandardCharsets.UTF_8);

            return Base64.decodeStr(ret,StandardCharsets.UTF_8);


        } catch (Exception ex) {
            ex.printStackTrace();
            return"";
        }
    }

    public static void main(String[] args) {
        String ret = SKeyEncrypt("8525285225", "65658414", "heelle world");
        log.info("加密结果---》"+ret);
        String dret = SKeyDecrypt("8525285225", "65658414", ret);

        log.info("解密结果---》"+dret);
    }

}
